<?php
require_once 'modules/content/models/Content.php';
require_once 'modules/content/models/ContentCategory.php';
require_once 'modules/language/models/Lang.php';
require_once 'modules/mail/models/Subscriber.php';
class front_footerSticker extends Nine_Sticker
{
	public function run()
	{
		$objContent = new Models_Content();
        $objLang    = new Models_Lang();
        $objCat     = new Models_ContentCategory();
        $objSubscriber = new Models_Subscriber();
		$email = @$_POST["email_subcriber"];
		if(null != $email) {
				if ($email == 'Nhập email nhận sản phẩm mới' || $email == 'Enter your email to receive new products' || $email == null){
					$this->session->errorEmail = 1;
					header("Location:". Nine_Registry::getAppBaseUrl() . "default/index/subscribe-success");
				}else{
					$pattern = '/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/';
					if(preg_match($pattern, $email)==0) {
						$this->session->errorEmail = 2;
				  		header("Location:". Nine_Registry::getAppBaseUrl() . "default/index/subscribe-success");
					}
					else{
						$data['email'] = $email;
						$name = explode("@", $data['email']);
						$data['name'] = $name[0];
						$a = $objSubscriber->getAll();
						foreach ($a  as $value) {
							if(trim($email) == trim($value['email']) ){
								$this->session->errorEmail = 4;
								header("Location:". Nine_Registry::getAppBaseUrl() . "default/index/subscribe-success");
								die;
								}
						}
						$this->session->errorEmail = 3;
						
						$data['created_date'] = time();
						$data['enabled'] = 1;
						
						$objSubscriber->insert($data);
						
						header("Location:". Nine_Registry::getAppBaseUrl() . "default/index/subscribe-success");
					}
				}
			}
	
	
	
        $about = $objContent->setAllLanguages(true)->getContentById(211);
        $support = $objContent->setAllLanguages(true)->getContentById(213);
        $counseling = $objContent->setAllLanguages(true)->getContentById(215); 
        $email = $objContent->setAllLanguages(true)->getContentById(217);
        $Commitment = $objContent->setAllLanguages(true)->getContentById(219);
        $copyright = $objContent->setAllLanguages(true)->getContentById(221);
        $this->view->about = $about;
        $this->view->support = $support;
        $this->view->counseling = $counseling;
        $this->view->email = $email;
        $this->view->commitment = $Commitment;
        $this->view->copyright = $copyright;
        $ip = $_SERVER['REMOTE_ADDR'];
    
	    $file_ip = fopen('counter/ip.txt', 'rb');
	    while (!feof($file_ip)) $line[]=fgets($file_ip,1024);
	    for ($i=0; $i<(count($line)); $i++) {
	        list($ip_x) = explode ("\n",$line[$i]);
	        if ($ip == $ip_x) {$found = 1;}
	    }
	    fclose($file_ip);
	    
	    if (!($found==1)) {
	        $file_ip2 = fopen('counter/ip.txt', 'ab');
	        $line = "$ip\n";
	        fwrite($file_ip2, $line, strlen($line));
	        $file_count = fopen('counter/count.txt', 'rb');
	        $data = '';
	        while (!feof($file_count)) $data .= fread($file_count, 4096);
	        fclose($file_count);
	        list($today, $yesterday, $total, $date, $days) = explode ("%", $data);
	        if ($date == date("Y m d")) $today++;
	            else {
	                $yesterday = $today;
	                $today = 1;
	                $days++;
	                $date = date("Y m d");
	            }
	        $total++;
	        $line = "$today%$yesterday%$total%$date%$days";
	        
	        $file_count2 = fopen('counter/count.txt', 'wb');
	        fwrite($file_count2, $line, strlen($line));
	        fclose($file_count2);
	        fclose($file_ip2);
	      }
	      
	       $this->view->online = $this->online();
	       $this->view->today = $this->today();
	       $this->view->total = $this->total() + 552480;
    }
	public function online()
		{
	    $rip = $_SERVER['REMOTE_ADDR'];
	    $sd = time();
	    $count = 1;
	    $maxu = 1;
	
	    $file1 = "counter/online.log";
	    $lines = file($file1);
	    $line2 = "";
	
	    foreach ($lines as $line_num => $line)
	    {
	        if($line_num == 0)
	        {
	            $maxu = $line;
	        }
	        else
	        {
	            $fp = strpos($line,'****');
	            $nam = substr($line,0,$fp);
	            $sp = strpos($line,'++++');
	            $val = substr($line,$fp+4,$sp-($fp+4));
	            $diff = $sd-$val;
	
	            if($diff < 300 && $nam != $rip)
	            {
	                $count = $count+1;
	                $line2 = $line2.$line;
	            }
	        }
	    }
	
	    $my = $rip."****".$sd."++++\n";
	    if($count > $maxu)
	    $maxu = $count;
	
	    $open1 = fopen($file1, "w");
	    fwrite($open1,"$maxu\n");
	    fwrite($open1,"$line2");
	    fwrite($open1,"$my");
	    fclose($open1);
	    $count=$count;
	    $maxu=$maxu+200;
	    
	    return $count;
	}

    
      
      
    public function today()
    {
        $file_count = fopen('counter/count.txt', 'rb');
        $data = '';
        while (!feof($file_count)) $data .= fread($file_count, 4096);
        fclose($file_count);
        list($today, $yesterday, $total, $date, $days) = explode ("%", $data);
        return $today;
    }
    public function yesterday()
    {
        $file_count = fopen('counter/count.txt', 'rb');
        $data = '';
        while (!feof($file_count)) $data .= fread($file_count, 4096);
        fclose($file_count);
        list($today, $yesterday, $total, $date, $days) = explode ("%", $data);
        return $yesterday;
    }
    public function total()
    {
        $file_count = fopen('counter/count.txt', 'rb');
        $data = '';
        while (!feof($file_count)) $data .= fread($file_count, 4096);
        fclose($file_count);
        list($today, $yesterday, $total, $date, $days) = explode ("%", $data);
        return $total;
    }
     public function avg()
    {
        $file_count = fopen('counter/count.txt', 'rb');
        $data = '';
        while (!feof($file_count)) $data .= fread($file_count, 4096);
        fclose($file_count);
        list($today, $yesterday, $total, $date, $days) = explode ("%", $data);
        echo ceil($total/$days);
    }
}